#include "csb.h"
#include "intrins.h"
sbit tx = P1^0;
sbit rx = P1^1;
static void Delay12us(void)	//@12.000MHz
{
	unsigned char data i;

	_nop_();
	_nop_();
	i = 33;
	while (--i);
}
void send_wave(void)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		tx = 1;
		Delay12us();
		tx = 0;
		Delay12us();
	}
}
unsigned int get_distance(void)
{
	unsigned int distance = 0;
	CMOD = 0x00;
	CL = CH = 0;
	send_wave();
	CR = 1;
	while(CF == 0 && rx == 1);
	CR = 0;
	if(CF == 1)
	{
		CF = 0;
		return 999;
	}
	distance = CH;
	distance = CH << 8 | CL;
	distance = (unsigned int)distance * 0.017;
	return distance;
}